/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Yohanes Beny P
 */
public class Pemesanan {
    
    private String kode_pemesanan;
    private String jmlh;
    private String tgl_pesan;
    private String kode_brg;
    private String id_cab;
    
    public Pemesanan() {
    }
    
    public Pemesanan(String kode_pemesanan, String jmlh, String tgl_pesan, String kode_brg, String id_cab) {
        this.kode_pemesanan = kode_pemesanan;
        this.jmlh = jmlh;
        this.tgl_pesan = tgl_pesan;
        this.kode_brg = kode_brg;
        this.id_cab = id_cab;
    }
    
    public String getKode_pemesanan() {
        return kode_pemesanan;
    }
    
    public void setKode_pemesanan(String kode_pemesanan) {
        this.kode_pemesanan = kode_pemesanan;
    }
    
    public String getJmlh() {
        return jmlh;
    }
    
    public void setJmlh(String jmlh) {
        this.jmlh = jmlh;
    }
    
    public String getTgl_pesan() {
        return tgl_pesan;
    }
    
    public void setTgl_pesan(String tgl_pesan) {
        String a = tgl_pesan.substring(0, 2);
        String b = tgl_pesan.substring(2, 4);
        String c = tgl_pesan.substring(4);
        this.tgl_pesan = a + "-" + b + "-" + c;
    }
    
    public String getKode_brg() {
        return kode_brg;
    }
    
    public void setKode_brg(String kode_brg) {
        this.kode_brg = kode_brg;
    }
    
    public String getId_cab() {
        return id_cab;
    }
    
    public void setId_cab(String id_cab) {
        this.id_cab = id_cab;
    }
    
    public String generateKodePemesanan(String date, String id) {
        String kode;
        kode = id + date + "0";
        return kode;
    }
    
    public String kodePemesenanFinal(String kode) throws SQLException {
        String code = null;
        String kod = kode.substring(0, 11);
        int a = 0;
        Connection con = DataBase.Koneksi.open();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select KODEPEMESANAN from PEMESANAN_FROGSTONE where KODEPEMESANAN like '" + kod + "%'");
        while (rs.next()) {
            String sementara = rs.getString("KODEPEMESANAN");
            String s2 = sementara.substring(11);
            a = Integer.parseInt(s2);
            a++;
        }
        return code = kod + String.valueOf(a);
    }
    
    public void tambahPemesanan(Pemesanan psn) throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement st = con.createStatement();
        String ins = "insert into PEMESANAN_FROGSTONE "
                + "values ('" + psn.getKode_pemesanan() + "','" + psn.getJmlh() + "','" + psn.getTgl_pesan()
                + "','" + psn.getKode_brg() + "','" + psn.getId_cab() + "')";
        st.executeQuery(ins);
    }
    
    public ResultSet tampilPemesanan() throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement sta = con.createStatement();
        ResultSet res = sta.executeQuery("select * from PEMESANAN_FROGSTONE");
        return res;
    }
    
    public ResultSet tampilPemesananKonfirm(String kode) throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement sta = con.createStatement();
        ResultSet res = sta.executeQuery("select * from PEMESANAN_FROGSTONE where KODEPEMESANAN='" + kode + "'");
        return res;
    }
    
    public void deletePesanan(String kode) throws SQLException {
        Connection con = DataBase.Koneksi.open();
        Statement st = con.createStatement();
        String sql = "delete from PEMESANAN_FROGSTONE where KODEPEMESANAN='" + kode + "'";
        st.executeUpdate(sql);
    }
}
